package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/strategycomment")
public class StrategyCommentController {

    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private ITagService  tagService;

    //查询状态为推荐的评论
    @GetMapping("/commend")
    @ResponseBody
    public Object  getByState(Model model, @ModelAttribute("qo") StrategyCommentQueryObject qo){
            JsonResult jsonResult = new JsonResult();
        try {
            List<StrategyComment> list = strategyCommentService.selectByState();
            jsonResult.setResult(list);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("失败");
            return jsonResult;
        }
    }
    //根据攻略ID找出所有对应评论
    @GetMapping("/{StrategyId}/list")
    @ResponseBody
    public Object  list(Model model, @ModelAttribute("qo") StrategyCommentQueryObject qo){
        qo.setOrderBy("sc.createTime desc");
        qo.setPageSize(100);
        PageInfo list = strategyCommentService.list(qo);
        return list;
    }
    //根据评论ID找
    @GetMapping("/{strategycommentId}")
    @ResponseBody
    public Object  getBystrategycommentId(@PathVariable("strategycommentId") Long strategycommentId){
        StrategyComment one = strategyCommentService.selectBystrategycommentId(strategycommentId);
        return one;
    }
    //评论新增接口
    @PostMapping
    @ResponseBody
    public JsonResult create(StrategyComment strategyComment,Long strategyId){
        JsonResult jsonResult = new JsonResult();
        try {
            Strategy strategy = new Strategy();
            strategy.setId(strategyId);
            strategyComment.setStrategy(strategy);
            strategyCommentService.saveOrUpdate(strategyComment);
            //标签保存 及维护关系
            List<Tag> tags = strategyComment.getTags();
            if (tags != null){
                for (Tag tag : tags) {
                    //先查询数据库是否存在此标签
                    Tag resultTag = tagService.selectByTagName(tag);
                    //不存在,保存
                    if (resultTag==null){
                        tagService.insert(tag);
                        resultTag = tag;
                    }
                    //维护评论与标签关系
                    tagService.insertRelation(strategyComment.getId(),resultTag.getId());
                    //维护攻略与标签关系
                    tagService.insertStrategyRelation(strategyId,resultTag.getId());
                }
            }
            jsonResult.setResult(strategy);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("服务器繁忙");
            return jsonResult;
        }
    }

}
